package com.zhiyou100.review.day011.baseclasslibrary;

/**
 * @packageName: RandomDate
 * @className: com.zhiyou100.review.day011.baseclasslibrary
 * @Description: TODO 随机日期
 * @author: yang
 * @date: 6/17/20
 */
public class RandomDate {

    /**
     * @name: addZero
     * @param: int num
     * @description: TODO  把 1 变成 01
     * @date: 2020/2/21 5:43 下午
     * @return: String
     */
    public static String addZero(int num) {

        int theUnit = 10;
        if (num / theUnit == 0) {
            // 1/10 =  0，是个位
            return "0" + num;
        } else {
            return String.valueOf(num);
            // Int -> String
        }
    }

    /**
     * @name: randomOfMinToMax
     * @param: double min,double max
     * @description: TODO  生成 [min,max]的随机数
     * @date: 2020/2/21 5:17 下午
     * @return: int
     */
    public static int randomOfMinToMax(double min, double max) {
        return (int) (min + (int) (Math.random() * (max - min + 1)));
    }

    /**
     * @name: judgeTheDaysOfTheMonth
     * @param: int year ,int month
     * @description: TODO  判断本月有多少天
     * @date: 2020/2/21 5:00 下午
     * @return: int
     */
    public static int judgeTheDaysOfTheMonth(int year, int month) {

        boolean yearLag = (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0));
        // yearLag true为闰年，false为平年
        /*
         * 平年的2月份是28天
         * 闰年的2月份是29天
         */
        int maxDay;
        switch (month) {
            case 1:
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12:
                maxDay = 31;
                break;
            case 2:
                if (yearLag) {
                    maxDay = 29;
                } else {
                    maxDay = 28;
                }
                break;
            default:
                maxDay = 30;
        }
        return maxDay;
    }


    /**
     * @Description: TODO  随机一个表示日期时间的字符串 格式：xxxx-xx-xx xx:xx:xx 随机的时间范围：2000-01-01 00:00:00 到2020-12-31 23:59:59
     * @name: randomDate
     * @param: []
     * @return: java.lang.String
     * @date: 6/17/20 20:24
     * @author: yang
     */
    public static String randomDate() {

        final String thickStick = "-";
        final String colon = ":";

        int randomOfYear = randomOfMinToMax(2000, 2020);
        // 年 [2000,2020]
        int randomOfMonth = randomOfMinToMax(1, 12);
        // 月 [1,12]
        int randomOfDay = randomOfMinToMax(1, judgeTheDaysOfTheMonth(randomOfYear, randomOfMonth));
        //天 [1,judgeTheDaysOfTheMonth(randomOfYear,randomOfMonth)]

        int randomHours = randomOfMinToMax(0, 23);
        // 小时 [0~23]
        int randomMinute = randomOfMinToMax(0, 59);
        // 分钟 [0,59]
        int randomSecond = randomOfMinToMax(0, 59);
        //秒 [0,59]
        return addZero(randomOfYear) + thickStick + addZero(randomOfMonth) + thickStick + addZero(randomOfDay) + " " + addZero(randomHours) + colon + addZero(randomMinute) + colon + addZero(randomSecond);
    }

    public static void main(String[] args) {
        System.out.println(randomDate());
    }


}
